package com.tadpole.music.presenter.me.yun;

import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.me.yun.YunSubmitIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 提交 蝌蚪员考试
 * Created By 孙岩
 * on 2021/2/9
 */
public class YunSubmitPresenter extends BasePresenter<YunSubmitIView> {
    @SuppressWarnings("unchecked")
    public void submit(String question_answers, String score, String exam_id, String works) {
        if (isViewAttached()) {
            String url = UrlConfig.yunSubmit;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.YUN_TOKEN, "");
            DataModel.request(ModelToken.POST_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .paramKey("question_answers", "score", "exam_id", "works")
                    .paramValue(question_answers, score, exam_id, works)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    getView().showYunSubmit();
                                } else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
